package com.platform.modular.util;


import com.platform.modular.feign.account.MerchantAccountFeignService;
import com.platform.modular.feign.account.MerchantFeignService;
import com.platform.modular.feign.model.entity.MerchantAccountEntity;
import com.platform.modular.feign.model.entity.MerchantEntity;
import com.platform.modular.model.account.vo.MerchantBaseVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

/**
 * @author yifei.han
 * @version 1.0
 * @className MiniprogramUtil
 * @description
 * @date 2022/3/12
 */
@Component
public class MiniprogramUtil {

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private MerchantFeignService merchantFeignService;

    @Autowired
    private MerchantAccountFeignService merchantAccountFeignService;

    public Long obtainMerchantId(HttpServletRequest request) {
        String appId = request.getHeader("appId");
        return obtainMerchantId(appId);
    }

    public Long obtainMerchantId(String appId) {
        Object object = redisUtil.get("accout:merchant_id:" + appId);
        if (object == null) {
            MerchantEntity merchantEntity = merchantFeignService.queryMerchantByAppId(appId);
            if (merchantEntity == null) {
                return null;
            }
            return merchantEntity.getId();
        }
        return (Long) object;
    }

    public MerchantEntity obtainMerchant(HttpServletRequest request) {
        String appId = request.getHeader("appId");
        return obtainMerchant(appId);
    }

    public MerchantEntity obtainMerchant(String appId) {
        return merchantFeignService.queryMerchantByAppId(appId);
    }

    public MerchantBaseVO obtainMerchantBase(HttpServletRequest request) {
        Long merchantId = obtainMerchantId(request);
        return merchantFeignService.obtainMerchantBase(merchantId);
    }

    public Long obtainAccountId(HttpServletRequest request) {
        Long accountId = (Long) request.getAttribute("accountId");
        return accountId;
    }

    public MerchantAccountEntity obtainExclusiveAdviser(HttpServletRequest request) {
        Long accountId = obtainAccountId(request);
        Long merchantId = obtainMerchantId(request);
        return merchantAccountFeignService.queryExclusiveAdviser(accountId, merchantId);
    }


}
